package cn.zebra.dev.shop.module.address.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.order.module.order.entity.OrdersEntity;
import cn.zebra.dev.other.module.shopAddress.entity.ShopAddressEntity;
import cn.zebra.dev.other.module.shopAddress.service.ShopAddressService;
import cn.zebra.dev.other.module.shopAfter.entity.ShopAfterEntity;
import cn.zebra.dev.shop.module.address.dto.ShopAddressDTO;
import cn.zebra.dev.shop.module.address.dto.ShopAddressDetailedDTO;
import cn.zebra.dev.shop.module.address.vo.ShopAddressVO;
import cn.zebra.dev.shop.module.order.dto.OrdersDTO;
import cn.zebra.dev.shop.module.shop.entity.ShopEntity;
import cn.zebra.dev.shop.module.shop.service.ShopService;
import cn.zebra.dev.shop.module.shop.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author feifeijunxin
 * @Date 2020/5/12 17:28
 * @Version 1.0
 */
@RestController
@Api(tags = "店铺退货地址管理")
@RequestMapping("/shop/address")
public class ShopAddressController {

    @Autowired
    private ShopAddressService shopAddressService;

    @Autowired
    private ShopService shopService;

    @GetMapping("/list")
    @ApiOperation("退货地址列表")
    public Result<PageUtils<ShopAddressDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<ShopAddressEntity> pageUtils = shopAddressService.findPage(params);
        PageUtils<ShopAddressDTO> data = new PageUtils<>(pageUtils);
        List<ShopAddressEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ShopAddressDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                ShopAddressDTO dto = new ShopAddressDTO();
                BeanUtils.copyProperties(entity, dto);
                dto.setDetailedAddress(entity.getProvince()+entity.getCity()+entity.getDetailedAddress());
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改退货地址信息")
    public Result update(@PathVariable Long id,@Valid @RequestBody ShopAddressVO vo) {
        ShopAddressEntity entity = new ShopAddressEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setId(id);
        entity.setUpdateTime(LocalDateTime.now());
        if (!shopAddressService.updateById(entity)){
            return Result.success("修改失败!");
        }
        return Result.success("修改成功!");
    }

    @PostMapping("/save")
    @ApiOperation("新增退货地址信息")
    public Result save(@Valid @RequestBody ShopAddressVO vo) {
        ShopAddressEntity entity = new ShopAddressEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setCreateTime(LocalDateTime.now());
        if (!shopAddressService.save(entity)){
            return Result.success("修改失败!");
        }
        return Result.success("修改成功!");
    }
    @PostMapping("/delete/{id}")
    @ApiOperation("删除退货地址信息")
    public Result delete(@PathVariable Long id) {
        ShopAddressEntity entity = new ShopAddressEntity();
        entity.setId(id);
        entity.setDeleteFalg(true);
        entity.setUpdateTime(LocalDateTime.now());
        if (!shopAddressService.updateById(entity)){
            return Result.success("删除失败!");
        }
        return Result.success("删除成功!");
    }

    @PostMapping("/detailed/{id}")
    @ApiOperation("查看退货地址详情")
    public Result<ShopAddressDetailedDTO> detailed(@PathVariable Long id) {
        ShopAddressEntity entity = shopAddressService.getById(id);
        ShopAddressDetailedDTO shopAddressDetailedDTO = new ShopAddressDetailedDTO();
        BeanUtils.copyProperties(entity, shopAddressDetailedDTO);
        if(entity.getShopId()==0){
            shopAddressDetailedDTO.setShopName("自营");
        }else{
            ShopEntity shopEntity = shopService.findById(entity.getShopId());
            shopAddressDetailedDTO.setShopName(shopEntity.getShopName());
        }
        return Result.success(shopAddressDetailedDTO);
    }
}
